﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace AAA.DataTransfer
{
    public class TransferHelper
    {        
        public static DataTable Group(DataTable dataSource, List<string> lstGroupList, List<ITranslate> lstTranslateList, DataTypeEnum eDataType)
        {
            Dictionary<string, IGroupAction> dicGroupAction = new Dictionary<string,IGroupAction>();
            IGroupAction groupAction = null;

            // 依資料型態產生預設的GroupAction
            switch (eDataType)
            {
                case DataTypeEnum.Double:
                    groupAction = new NumericGroupAction();
                    break;

                case DataTypeEnum.Int:
                    groupAction = new NumericGroupAction("0");
                    break;
            
                case DataTypeEnum.String:
                    groupAction = new StringGroupAction(",");
                    break;                                    
            }

            for (int i = 0; i < dataSource.Columns.Count; i++)
            {
                if (lstGroupList.IndexOf(dataSource.Columns[i].ColumnName) >= 0)
                    continue;
                dicGroupAction.Add(dataSource.Columns[i].ColumnName, groupAction);
            }

            return Group(dataSource, lstGroupList, lstTranslateList, dicGroupAction);
        }

        public static DataTable Group(DataTable dataSource, List<string> lstGroupList, List<ITranslate> lstTranslateList, Dictionary<string, IGroupAction> dicGroupAction)
        {
            DataTable dataTableResult = new DataTable();
            string strGroupName;

            for (int i = 0; i < dataSource.Columns.Count; i++)
                dataTableResult.Columns.Add(dataSource.Columns[i].ColumnName, dataSource.Columns[i].DataType);

            Dictionary<string, int> dicRowMapping = new Dictionary<string, int>();
            DataRow dataRow;
            int iRowIndex;
            try
            {
                for (int i = 0; i < dataSource.Rows.Count; i++)
                {
                    strGroupName = "";
                    for (int j = 0; j < lstGroupList.Count; j++)
                        strGroupName += "," + lstTranslateList[j].Translate(dataSource.Rows[i][lstGroupList[j]]);

                    if (!dicRowMapping.ContainsKey(strGroupName))
                    {
                        dataRow = dataTableResult.NewRow();

                        for (int j = 0; j < lstGroupList.Count; j++)
                            dataRow[lstGroupList[j]] = lstTranslateList[j].Translate(dataSource.Rows[i][lstGroupList[j]]);

                        foreach (string strColumnName in dicGroupAction.Keys)
                            dataRow[strColumnName] = dataSource.Rows[i][strColumnName];

                        dicRowMapping.Add(strGroupName, dataTableResult.Rows.Count);
                        dataTableResult.Rows.Add(dataRow);
                        continue;
                    }

                    iRowIndex = dicRowMapping[strGroupName];
                    dataRow = dataTableResult.Rows[iRowIndex];

                    foreach (string strColumnName in dicGroupAction.Keys)                    
                        dataRow[strColumnName] = dicGroupAction[strColumnName].Group(dataRow[strColumnName], dataSource.Rows[i][strColumnName]);    
                }
            }
            catch (Exception ex)
            {
                throw;
            }

            return dataTableResult;
        }
    }
}
